package com.xk.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.core.enums.HttpStatusResponse;
import com.cms.common.core.web.domain.Response;
import com.xk.domain.dto.CollegeClassDto;
import com.xk.domain.dto.CreateClassDto;
import com.xk.domain.dto.PageDTO;
import com.xk.domain.vo.CollegeClassVo;
import com.xk.entity.CollegeClass;
import com.xk.mapper.CollegeClassMapper;
import com.xk.service.CollegeClassService;
import com.xk.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.function.Function;

/**
 * 班级表(CollegeClass)表服务实现类
 *
 * @author YaWu
 * @since 2024-12-14 23:59:51
 */
@Service("collegeClassService")
public class CollegeClassServiceImpl extends ServiceImpl<CollegeClassMapper, CollegeClass> implements CollegeClassService {

    @Override
    @Transactional
    public Response createClass(CreateClassDto  createClassDto) {
        CollegeClass collegeClass = BeanCopyUtils.copyBean(createClassDto, CollegeClass.class);
        // 保存并返回结果
        try {
            this.save(collegeClass);
        }
        catch (Exception e) {
            return Response.error("保存班级失败");
        }
        return Response.success("保存班级成功");
    }

    @Override
    public PageDTO<CollegeClassVo> exportClass(CollegeClassDto collegeClassDto) {
        //分页查询
        Page<CollegeClass> page = collegeClassDto.toMpPage();
        //条件查询
        // 构建查询条件和分页对象
        LambdaQueryWrapper<CollegeClass> wrapper = Wrappers.lambdaQuery(CollegeClass.class)
                .like(StringUtils.isNotBlank(collegeClassDto.getCollegeName()), CollegeClass::getCollegeName, collegeClassDto.getCollegeName())
                .eq(collegeClassDto.getCollegeVlaue() != null, CollegeClass::getCollegeVlaue, collegeClassDto.getCollegeVlaue());
        //执行分页查询
        this.page(page, wrapper);
        return PageDTO.of(page, e -> {
            CollegeClassVo classVo = BeanCopyUtils.copyBean(e, CollegeClassVo.class);
            classVo.setCreateTime( DateUtil.formatDateTime(e.getCreateTime()));
            return classVo;
        });
    }
}

